from typing import List
import time

class Solution:
    def solveNQueens(self, n: int) -> List[List[str]]:
        def generateBoard():
            board = list()
            for i in range(n):
                row[queens[i]] = "Q"
                board.append("".join(row))
                row[queens[i]] = "."
            return board

        def backtrack(row: int):
            if row == n:
                board = generateBoard()
                solutions.append(board)
            else:
                for i in range(n):
                    if i in columns or row - i in diagonal1 or row + i in diagonal2:
                        continue
                    queens[row] = i
                    columns.add(i)
                    diagonal1.add(row - i)
                    diagonal2.add(row + i)
                    backtrack(row + 1)
                    columns.remove(i)
                    diagonal1.remove(row - i)
                    diagonal2.remove(row + i)

        solutions = list()
        queens = [-1] * n
        columns = set()
        diagonal1 = set()
        diagonal2 = set()
        row = ["."] * n
        backtrack(0)
        return solutions


t1 = time.time()
solution = Solution()
solutionsArray = []
for i in range(15):
    solutionsText = solution.solveNQueens(i)
    solutionsArray.append(len(solutionsText))
print(solutionsArray[len(solutionsArray) - 1])
print( time.time() - t1)

## PYTHON3.8 4800U 15次，为75.104秒
#  JAVA大概是7.425秒
#  C++大概是9.287秒
#  C# 大概是8.593秒

## Ryzen 7 7745HX
## PYTHON3.8 15次，为36秒
#  JAVA大概是4.152秒
#  C++大概是5.651秒
#  C# 大概是5.88秒
#  C# AOT 大概是4.5秒
#  luajit 大概是51秒
#  lua 大概是201秒
#  go 大概是6.74秒
#  JS 大概是14.5秒  CHROMIUM 67
#  JS 大概是26秒  EDGE-V122


# 使用位运算优化后的性能排行：
# JAVA 163ms
# c# AOT 160ms
# C# JIT 280ms
# C++ X64 1600MS
# C++ X64 优化算法后 160MS